package com.xmy.cultivate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xmy.cultivate.entity.EnrollInfo;
import com.xmy.cultivate.entity.EnrollInfoGrantSchool;
import com.xmy.cultivate.entity.Quarter;
import com.xmy.cultivate.entity.views.EnrollGrantCount;
import com.xmy.cultivate.mapper.EnrollInfoMapper;
import com.xmy.cultivate.mapper.RecordCourseMapper;
import com.xmy.cultivate.service.IEnrollInfoGrantSchoolService;
import com.xmy.cultivate.service.IEnrollInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmy.cultivate.service.IQuarterService;
import com.xmy.cultivate.service.IRecordCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 招生数据 服务实现类
 * </p>
 *
 * @author hpiggy
 * @since 2024-03-20
 */
@Service
public class EnrollInfoServiceImpl extends ServiceImpl<EnrollInfoMapper, EnrollInfo> implements IEnrollInfoService {

    @Autowired
    EnrollInfoMapper enrollInfoMapper;

    @Autowired
    IRecordCourseService iRecordCourseService;

    @Autowired
    RecordCourseMapper recordCourseMapper;

    @Autowired
    IEnrollInfoGrantSchoolService iEnrollInfoGrantSchoolService;

    @Autowired
    IQuarterService iQuarterService;

    @Override
    public Integer updateDeleted(Long id, Integer deleted){
        return enrollInfoMapper.updateDeleted(id,deleted);
    }


    @Override
    public boolean updateIsTrue(Long studentId,Long subjectsId){
        Quarter quarterNow = iQuarterService.getNowQuarter();
        QueryWrapper<EnrollInfo> enrollInfoQueryWrapper = new QueryWrapper<>();
        enrollInfoQueryWrapper.ge("add_date","2024-08-04");
        enrollInfoQueryWrapper.eq("student_id",studentId);
        enrollInfoQueryWrapper.eq("subjects_id",subjectsId);
        //enrollInfoQueryWrapper.eq("year_part",quarterNow.getYearPart());
        //enrollInfoQueryWrapper.eq("quarter_num",quarterNow.getNum());
        enrollInfoQueryWrapper.eq("is_true",0);
        enrollInfoQueryWrapper.eq("is_return",0);
        enrollInfoQueryWrapper.groupBy("order_id");
        List<EnrollInfo> enrollInfoList = enrollInfoMapper.getEnrollInfoList(enrollInfoQueryWrapper);
        for (EnrollInfo enrollInfo:enrollInfoList){
            this.updateEnrollIsTrue(enrollInfo,quarterNow.getYearPart(),quarterNow.getNum());
        }
        return true;
    }

    @Transactional
    public void updateEnrollIsTrue(EnrollInfo enrollInfo,Integer yearPart,Integer quarterNum){
        try {
            Integer count = iRecordCourseService.getRecordCountArrive(yearPart,quarterNum,enrollInfo.getStudentId(),enrollInfo.getSubjectsId());
            LocalDateTime nowDateTime = LocalDateTime.now().withNano(0);
            if (count>=2){
                UpdateWrapper<EnrollInfo> updateWrapper = new UpdateWrapper<>();
                updateWrapper.eq("order_id",enrollInfo.getOrderId());
                updateWrapper.eq("is_true",0);
                updateWrapper.eq("is_return",0);
                updateWrapper.set("is_true",1);
                updateWrapper.set("true_date_time",nowDateTime);
                this.update(updateWrapper);

                List<EnrollGrantCount> enrollGrantCountList =  recordCourseMapper.getEnrollGrantCount(yearPart,quarterNum,enrollInfo.getSubjectsId(),enrollInfo.getStudentId(),1);
                if (enrollGrantCountList.size() > 0){
                    //先删除，因为会存在已发放状态因些不能删除
                    //QueryWrapper<EnrollInfoGrantSchool> enrollInfoGrantSchoolQueryWrapper = new QueryWrapper<>();
                    //enrollInfoGrantSchoolQueryWrapper.eq("order_id",enrollInfo.getOrderId());
                    //enrollInfoGrantSchoolQueryWrapper.eq("order_id",enrollInfo.getOrderId());
                    //iEnrollInfoGrantSchoolService.remove(enrollInfoGrantSchoolQueryWrapper);
                }
                BigDecimal baseNum = new BigDecimal(1);
                BigDecimal countNum = new BigDecimal(enrollGrantCountList.size());
                BigDecimal grantNum = baseNum.divide(countNum,1,BigDecimal.ROUND_HALF_UP);
                List<Long> schoolIdList = new ArrayList<>();
                for (EnrollGrantCount enrollGrantCount:enrollGrantCountList){
                    QueryWrapper<EnrollInfoGrantSchool> enrollInfoGrantSchoolQueryWrapper = new QueryWrapper<>();
                    enrollInfoGrantSchoolQueryWrapper.eq("order_id",enrollInfo.getOrderId());
                    enrollInfoGrantSchoolQueryWrapper.eq("school_id",enrollGrantCount.getSchoolId());
                    enrollInfoGrantSchoolQueryWrapper.last("limit 1");
                    EnrollInfoGrantSchool enrollInfoGrantSchool = iEnrollInfoGrantSchoolService.getOne(enrollInfoGrantSchoolQueryWrapper);
                    if (enrollInfoGrantSchool !=null){
                        enrollInfoGrantSchool.setGrantNum(grantNum);
                        iEnrollInfoGrantSchoolService.updateById(enrollInfoGrantSchool);
                    }else {
                        EnrollInfoGrantSchool enrollInfoGrantSchoolAdd = new EnrollInfoGrantSchool();
                        enrollInfoGrantSchoolAdd.setSchoolId(enrollGrantCount.getSchoolId());
                        enrollInfoGrantSchoolAdd.setOrderId(enrollInfo.getOrderId());
                        enrollInfoGrantSchoolAdd.setGrantNum(grantNum);
                        iEnrollInfoGrantSchoolService.save(enrollInfoGrantSchoolAdd);
                    }
                    schoolIdList.add(enrollGrantCount.getSchoolId());
                }
                if (schoolIdList.size()>0){
                    QueryWrapper<EnrollInfoGrantSchool> enrollInfoGrantSchoolQueryWrapper = new QueryWrapper<>();
                    enrollInfoGrantSchoolQueryWrapper.eq("order_id",enrollInfo.getOrderId());
                    enrollInfoGrantSchoolQueryWrapper.notIn("school_id",schoolIdList);
                    iEnrollInfoGrantSchoolService.remove(enrollInfoGrantSchoolQueryWrapper);
                }
            }
        }catch (Exception e){
            System.out.println("错误"+e.getMessage());
        }
    }
}
